﻿using CXW.Core.Services;
using log4net;
using log4net.Config;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;

namespace CXW.Core.Sda
{
    /// <summary>日志记录类</summary>
    public sealed class log4netLoggingService : ILoggingService
    {
        ILog log;
        /// <summary>日志记录类构造函数
        /// 应用程序域的配置文件 appSettings 中 Log4NetConfigFile 配置项指定 log4net 配置文件,
        /// 如果节点指定的文件不存在则引发<see cref="InvalidOperationException"/>异常,
        /// 如果节点未指定配置文件或者没有该配置节点则使用应用程序域的配置文件
        /// </summary>
        public log4netLoggingService()
        {
            string configFile = ConfigurationManager.AppSettings["Log4NetConfigFile"];
            if (string.IsNullOrWhiteSpace(configFile))
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
            }
            else
            {
                FileInfo fInfo = new FileInfo(configFile);
                if (fInfo.Exists)
                {
                    XmlConfigurator.ConfigureAndWatch(fInfo);
                }
                else
                {//Exception.Log4NetConfigFile:Log4NetConfigFile Configuration error.
                    string exception = ResourceService.GetString("Exception.Log4NetConfigFile");
                    if (exception == "Exception.Log4NetConfigFile") { exception = "Log4NetConfigFile Configuration error ."; }
                    throw new InvalidOperationException(exception);
                }
            }
            log = LogManager.GetLogger(typeof(log4netLoggingService));
        }
        /// <remark/>
        public void Debug(object message)
        {
            log.Debug(message);
        }
        /// <remark/>
        public void DebugFormatted(string format, params object[] args)
        {
            log.DebugFormat(CultureInfo.InvariantCulture, format, args);
        }
        /// <remark/>
        public void Info(object message)
        {
            log.Info(message);
        }
        /// <remark/>
        public void InfoFormatted(string format, params object[] args)
        {
            log.InfoFormat(CultureInfo.InvariantCulture, format, args);
        }
        /// <remark/>
        public void Warn(object message)
        {
            log.Warn(message);
        }
        /// <remark/>
        public void Warn(object message, Exception exception)
        {
            log.Warn(message, exception);
        }
        /// <remark/>
        public void WarnFormatted(string format, params object[] args)
        {
            log.WarnFormat(CultureInfo.InvariantCulture, format, args);
        }
        /// <remark/>
        public void Error(object message)
        {
            log.Error(message);
        }
        /// <remark/>
        public void Error(object message, Exception exception)
        {
            log.Error(message, exception);
        }
        /// <remark/>
        public void ErrorFormatted(string format, params object[] args)
        {
            log.ErrorFormat(CultureInfo.InvariantCulture, format, args);
        }
        /// <remark/>
        public void Fatal(object message)
        {
            log.Fatal(message);
        }
        /// <remark/>
        public void Fatal(object message, Exception exception)
        {
            log.Fatal(message, exception);
        }
        /// <remark/>
        public void FatalFormatted(string format, params object[] args)
        {
            log.FatalFormat(CultureInfo.InvariantCulture, format, args);
        }
        /// <remark/>
        public bool IsDebugEnabled
        {
            get { return log.IsDebugEnabled; }
        }
        /// <remark/>
        public bool IsInfoEnabled
        {
            get { return log.IsInfoEnabled; }
        }
        /// <remark/>
        public bool IsWarnEnabled
        {
            get { return log.IsWarnEnabled; }
        }
        /// <remark/>
        public bool IsErrorEnabled
        {
            get { return log.IsErrorEnabled; }
        }
        /// <remark/>
        public bool IsFatalEnabled
        {
            get { return log.IsFatalEnabled; }
        }
    }
}